package com.lanren.familybookapp.ui.me.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.app.UserInfoBean;
import com.lanren.familybookapp.ui.me.interfaces.IEmailModel;

import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName EmailModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/11/4 17:57
 * @Version 1.0
 */
public class EmailModel extends BaseModel {
    public EmailModel() {
        initDispoable();
    }

    public void getEmailCode(String email, final IEmailModel.EmailModel emailModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("userEmail", email);
        Disposable disposable = HttpManager.post(ApiUrl.API.USER_INFO_EMAIL_CODE)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        emailModel.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(String s) {
                        emailModel.onSuccess(s);
                    }
                });
        addDisposable(disposable);
    }

    public void addEmailCode(String email, String emailCode, final IEmailModel.EmailModel emailModel) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("userEmail", email);
        map.put("emailCode", emailCode);
        Disposable disposable = HttpManager.post(ApiUrl.API.USER_INFO_EMAIL_ADD)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        emailModel.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        AppUserHelper.getInstance().setUserInfoBean(userInfoBean);
                        emailModel.onSuccess("邮箱绑定成功");
                    }
                });
        addDisposable(disposable);
    }
}
